<template>
	<view>
		<view class="cell">
			<view class="label">自我描述</view>
			<view class="cell-item">
				<view class="tag" :class="{'selected':1==item.isselect}" v-for="(item, index) in gxTags" @click="onItemClick(item, index)">{{item.name}}</view>
			</view>
		</view>
		
		<view class="cell">
			<view class="label">兴趣爱好</view>
			<view class="cell-item">
				<view class="tag" :class="{'selected':1==item.isselect}" v-for="(item, index) in xqTags" @click="onItemClick(item, index)">{{item.name}}</view>
			</view>
		</view>
		
		<view class="btn" @click="requestSave">保存</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userId: '',
				gxTags: [],
				xqTags: []
			};
		},
		
		onLoad(option) {
			if(option){
				this.userId = uni.getStorageSync('userId');
				this.requestList();
			}
		},
		methods:{
			async requestList(){
				let res = await this.$common.post("/app/user/getBiaoqian");
				if(0 == res.code){
					let data = res.data;
					this.gxTags = data.gexing;
					this.xqTags = data.xingqu;
					console.log("this.gxTags: " + JSON.stringify(this.gxTags));
				}else{
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			async requestSave(){
				let ids = '';
				let names = '';
				this.gxTags.forEach(item=>{
					if(1==item.isselect){
						if('' == ids){
							ids = item.id;
							names = item.name;
						}else{
							ids += ','+item.id;
							names += ','+item.name;
						}
					}
				})
				this.xqTags.forEach(item=>{
					if(1==item.isselect){
						if('' == ids){
							ids = item.id;
							names = item.name;
						}else{
							ids += ','+item.id;
							names += ','+item.name;
						}
					}
				})
				
				if('' == ids){
					uni.showToast({
						title:'请选择',
						icon: 'none'
					})
					return
				}
				
				let res = await this.$common.post("/app/geren/upBiaoqian", {biaoqian: ids});
				if(0 == res.code){
					let data = res.data;
					uni.showToast({
						title: '添加成功',
						icon: 'none'
					})
					setTimeout(()=>{
						uni.navigateBack({
							success:res=>{
							  const eventChannel = this.getOpenerEventChannel();
							  eventChannel.emit('sendTags', { names: names })
							}
						});
					}, 500)
				}else{
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			
			onItemClick(item, index){
				console.log("item: " + JSON.stringify(item));
				console.log("index: " + JSON.stringify(index));
				item.isselect = item.isselect == 1? 2: 1;
			}
		}
	}
</script>

<style lang="scss">
	
	.cell{
		display: flex;
		flex-direction: column;
		background-color: #fff;
		border-radius: 15rpx;
		padding: 20rpx;
		margin: 20rpx;
		.label{
			font-size: 36rpx;
			color: #222;
		}
		.cell-item{
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			.tag{
				padding: 10rpx 20rpx;
				background-color: #eee;
				border-radius: 50rpx;
				font-size: 24rpx;
				color: #555;
				margin: 10rpx;
			}
			.selected{
				background-color: #009fff;
				color: #fff;
			}
		}
		
	}
	
	.btn{
		height: 80rpx;
		line-height: 80rpx;
		margin: 30rpx;
		background-image: linear-gradient(to right, #9fe9ff, #ab7aff);
		border-radius: 40rpx;
		text-align: center;
		color: #fff;
	}

</style>
